package com.lanou.myim.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.R;
import com.lanou.myim.db.ApplyEty;
import com.lanou.myim.db.ApplyEtyDao;
import com.lanou.myim.tools.DaoSingleton;
import com.lanou.myim.tools.ParseSingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by dllo on 16/3/10.
 */
public class ApplyListviewAdapter extends BaseAdapter{
    private Context context;
    private ApplyEtyDao applyEtyDao;
    private List<ApplyEty> datas;
    public ApplyListviewAdapter(Context context){
        this.context = context;
        applyEtyDao = DaoSingleton.getInstance().getApplyEtyDao();
        List<ApplyEty> applyEties = applyEtyDao.queryBuilder().list();
        datas = new ArrayList<>();
        for (ApplyEty applyEty : applyEties){
            if (applyEty.getAccount().equals(EMChatManager.getInstance().getCurrentUser())){
                datas.add(applyEty);
            }
        }

    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_apply,null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (MyViewHolder) convertView.getTag();
        }
        ParseSingleton.setBitmap(datas.get(position).getUserName(), holder.headIv);
        holder.userNameTv.setText(datas.get(position).getUserName());
        holder.reasonTv.setText(datas.get(position).getReason());
        if (datas.get(position).getAnswer()){
            holder.answerBtn.setVisibility(View.GONE);
            holder.agreeTv.setVisibility(View.VISIBLE);
        }else {
            holder.agreeTv.setVisibility(View.GONE);
            holder.answerBtn.setVisibility(View.VISIBLE);
            final MyViewHolder finalHolder = holder;
            holder.answerBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finalHolder.answerBtn.setVisibility(View.GONE);
                    finalHolder.agreeTv.setVisibility(View.VISIBLE);
                    datas.get(position).setAnswer(true);
                    applyEtyDao.insertOrReplace(datas.get(position));
                    try {
                        EMChatManager.getInstance().acceptInvitation(datas.get(position).getUserName());
                    } catch (EaseMobException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return convertView;
    }

    class MyViewHolder{
        ImageView headIv;
        TextView userNameTv;
        TextView reasonTv;
        TextView agreeTv;
        Button answerBtn;
        MyViewHolder(View itemView){
            headIv = (ImageView) itemView.findViewById(R.id.head_iv);
            userNameTv = (TextView) itemView.findViewById(R.id.user_name_tv);
            reasonTv = (TextView) itemView.findViewById(R.id.reason_tv);
            agreeTv = (TextView) itemView.findViewById(R.id.agree_tv);
            answerBtn = (Button) itemView.findViewById(R.id.agree_btn);
        }
    }
}
